#!/bin/bash

# 参数： 服务名、版本号
S_NAME=sojson-ui
S_VERSION=1.0.0

# 判断原镜像是否存在
S_IMG=$(docker images -q --filter reference=hub.unionlive.com/$S_NAME*:*)
# 判断服务是否存在，存在更新镜像，不存在创建
S_HUB="hub.unionlive.com/$S_NAME:$S_VERSION"
# 查询是否有运行该项目的镜像
SID=$(docker ps -qf name=$S_NAME)
DOCKER_PULL="docker pull $S_HUB"
DOCKER_RUN="docker run -it -d -p 58888:80 --name $S_NAME $S_HUB"
if [ "x" == "x$SID" ] ; then
# 如果没有，则直接运行
    echo "> $DOCKER_PULL"
    eval $DOCKER_PULL
    echo "> $DOCKER_RUN"
    eval $DOCKER_RUN
else
# 如果有，则删除该正在运行的镜像
    DOCKER_STOP="docker stop $SID"
    echo "> $DOCKER_STOP"
    eval $DOCKER_STOP
    DOCKER_RM="docker rm $SID"
    echo "> $DOCKER_RM"
    eval $DOCKER_RM
    DOCKER_IMAGE_RM="docker image rm $S_IMG"
    echo "> $DOCKER_IMAGE_RM"
    eval $DOCKER_IMAGE_RM
    echo "> $DOCKER_PULL"
    eval $DOCKER_PULL
    echo "> $DOCKER_RUN"
    eval $DOCKER_RUN
fi

echo "build start success ......"
